package com.example.campus;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.campus.utils.Http;
import com.campus.utils.WebViewUtils;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class Web extends Activity {
	WebView web;
	String select;
	String header;

	public void onCreate(Bundle b) {
		super.onCreate(b);
		web = new WebView(this);

		header = getIntent().getStringExtra("data").substring(0,
				getIntent().getStringExtra("data").indexOf('/', 8));
		web.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
		web.getSettings().setLoadWithOverviewMode(true);
		web.getSettings().setJavaScriptEnabled(true);
		web.setWebViewClient(new WebViewClient() {
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {

				Intent i = new Intent();
				i.setData(Uri.parse(header + url));
				startActivity(i);
				return true;
			}
		});
		setContentView(web);
		new Thread() {
			public void run() {
				Document doc = null;
				try {
					doc = Jsoup.parse(new URL(getIntent()
							.getStringExtra("data")), 10000 * 10);
				} catch (MalformedURLException e2) {
					// TODO Auto-generated catch block
					e2.printStackTrace();
				} catch (IOException e2) {
					// TODO Auto-generated catch block
					e2.printStackTrace();
				}
				Elements e = doc.select("div#xaufe_middiv");
				final String okresp = e.toString();
				String u = null;
				try {
					u = Http.get(getIntent().getStringExtra("data"), "GBK");
				} catch (MalformedURLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				final String resp = u;
				runOnUiThread(new Runnable() {

					@Override
					public void run() {
						// TODO Auto-generated method stub
						String load = null;
						try {
							if (getIntent().getStringExtra("data").startsWith(
									"http://www.xaufe.edu.cn")) {

								System.out.println(okresp);
								// load=WebViewUtils.handle(okresp);\
								load = okresp;
							} else
								load = WebViewUtils.okresp(resp, "div#web2l");

						} catch (MalformedURLException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						web.loadDataWithBaseURL(null, load, "text/html",
								"utf-8", null);
					}
				});
			}
		}.start();

	}

	static void run(Context context, String url) {

		Intent i = new Intent(context, Web.class);
		i.putExtra("data", url);
		context.startActivity(i);
	}
}
